package org.codesandtags.cstutoringcenter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Problem22 {
	
	File file;
	
	public static void main(String[] args) {
		
		Problem22 solve = new Problem22("src/test/resources/plane22.txt");
		System.out.println("Result : " + solve.getMidPointTotal());
		
	}
	
	public Problem22(String f) {
		file = new File(f);
		if(!file.exists()){
			System.err.println(" ERROR : El archivo no existe");
		}
	}
	
	public String getMidPointTotal(){
		
		double mid1 = 0;
		double mid2 = 0;
		
		try{
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			
			while( (line = br.readLine()) != null){
				StringTokenizer st = new StringTokenizer(line,"|");
				String coords1[] = st.nextToken().split(","); 
				String coords2[] = st.nextToken().split(",");
				
				double x1 = Double.parseDouble(coords1[0]);
				double y1 = Double.parseDouble(coords1[1]);
				double x2 = Double.parseDouble(coords2[0]);
				double y2 = Double.parseDouble(coords2[1]);
				
				mid1 += (y1 + y2) / 2;
				mid2 += ((x2 + y2) / 2);
				System.out.println("m1 : " + coords1[0] + "," + coords1[1] + " >> " + mid1);
				System.out.println("m2 : " + coords2[0] + "," + coords2[1] + " >> " + mid2);
				
				
			}
		
		}catch(IOException e){
			e.printStackTrace();
		}
		
		
		return mid1 + ", " + mid2;
	}
	

}
